package com.itheima.run;

import com.itheima.Utils.Utils;
import com.itheima.domain.Student;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void studentmanager(ArrayList<Student> list) {
        while (true) {

            System.out.println("=====================================================");
            System.out.println("【学生信息管理】");
            System.out.println();
            System.out.println("1.添加学生  2.修改学生  3.查询学生  4.删除学生  5.返回");
            System.out.println("=====================================================");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入菜单编码:");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    addstudent(list);
                    break;
                case 2:
                    updatestudent(list);
                    break;
                case 3:
                    findstudent(list);
                    break;
                case 4:
                    removestudeng(list);
                    break;
                case 5:
                    return;
            }
        }
    }
    //添加学生方法;
    public static void addstudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生性别:");
        String sex = sc.next();
        System.out.println("请输入学生生日:");
        String birthday = sc.next();
        //从Ulils工具类中调用获取id方法获取id;
        int stuId = ++Utils.stuId;
        //从Utils工具类中调取计算年龄的方法获取年龄;
        int age = Utils.birthdayToAge(birthday);
        //将所有数据封装到对象当中;
        Student stu =  new Student(stuId,name,sex,birthday,age);
        //调用集合add方法将对象添加到集合当中;
        list.add(stu);
        System.out.println("添加信息成功");
    }
    //查看学生方法;
    public static void findstudent(ArrayList<Student> list){
        //首先判断集合中有没有元素,如果没有就打印没有内容;
        if(list.size()==0){
            System.out.println("没有您所查找的编号.");
        }else{
            //运行到这里就说明有元素,调用Utils工具类中的打印方法打印集合元素;
            Utils.printPersonList(list);
        }
    }
    //删除学生方法;
    public static void removestudeng(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生编号:");
        int id = sc.nextInt();
        for (int i = 0; i < list.size(); i++) {
            //取出集合中的每一个对象;
            Student student = list.get(i);
            //调用get方法获取每一个对象中的id值;
            int id1 = student.getId();
            //判断,如果id相同说明找到对象了;
            if(id==id1){
                //找到就删除该元素;
                list.remove(i);
                System.out.println("删除成功;");
                return;
            }
        }
        //如果代码运行到了这里,说明集合中没有找到所需要的的内容;
        System.out.println("没有您所查找的编号;");

    }
    //修改学生方法;
    public static void updatestudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生编号:");
        int id = sc.nextInt();
        for (int i = 0; i < list.size(); i++) {
            //取出集合中的每一个对象;
            Student student = list.get(i);
            //调用get方法获取每一个对象中的id值;
            int id1 = student.getId();
            if(id==id1){
                System.out.println("请输入新的学生姓名(不变输入0);");
                String name = sc.next();
                System.out.println("请输入新的学生性别(不变输入0);");
                String sex = sc.next();
                System.out.println("请输入新的学生生日(不变输入0);");
                String birthday = sc.next();
                if(!name.equals("0")){
                    student.setName(name);
                }
                if(!sex.equals("0")){
                    student.setSex(sex);
                }
                if(!birthday.equals("0")){
                    student.setBirthday(birthday);
                    //从Utils工具类中调取计算年龄的方法获取年龄;
                    int age = Utils.birthdayToAge(birthday);
                    //将重新计算好的值添加到对象当中去;
                    student.setAge(age);
                }
            }
            System.out.println("信息修改成功.");
            return;
        }
        System.out.println("没有您所查找的编号.");

    }
}
